<template>
	<view class="view-height">
		<view class="tn-margin-sm">
			<tn-swiper :list="banners" imageMode="scaleToFill" :height="300" :effect3d="false" mode="none" @click="swiperClick"></tn-swiper>
		</view>
		<view class="content flex">
			<view class="content_left flex justify-center">
				<view class="cu-chat align-center textFont">
					<text :class="['underline',{'active': isActive === item.id}]" v-for="item in leftNav" :key="item.id"
						@click="NavTitle(item.id)">{{item.title}}</text>
				</view>
			</view>
			<view class="content_right">
				<!-- <view class="flex justify-center"> -->
				<!-- <view class="search">
					<input type="text" placeholder="搜索" v-model="searchKeyword" @confirm="search" @input="search" />
					<text class="cuIcon-search Icon-search"></text>
				</view> -->
				<view style="border-bottom: 0.5rpx solid lightgray; padding-bottom: 15rpx;" v-if="isActive==1">
					<text style="color: #a8a8a8;font-size: 25rpx;">根据试题考试结果推荐</text>
				</view>
				<!-- </view> -->
				<scroll-view scroll-y class="heightScroll" @scrolltolower="scrollBottom">
					<view class="flex content_right_chapter margin-top-sm justify-between align-center" v-for="item in ChapterList"
						:key="item.id">
						<view class="chapter_img">
							<image :src="item.xiluedu_coupon.thumb_image"></image>
						</view>
						<view class="cu-chat textDetails">
							<text class="lablename"><text class="lable">名称：</text>《{{item.name}}》</text>
							<text><text class="lable">章节：</text>第{{item.weigh}}章</text>
							<text><text class="lable">老师：</text>{{item.xiluedu_coupon.author}}</text>
						</view>
						<button class="cu-btn round sm bg-green shadow" @click="CoureseStudy(item.id)">去学习</button>
					</view>
					<uni-load-more v-if="ChapterList.length>=10" :status="loading"></uni-load-more>
				</scroll-view>
			</view>
		</view>
		<login ref="login" @succ="succ"></login>
		<view class='tn-tabbar-height'></view>
		<tabbar></tabbar>
	</view>
</template>

<script>
	import { getChapterList ,CourseDetails } from '@/api/index.js'
	export default {
		data() {
			return {
				banners: [
					{
						image: 'https://shizheserver.oss-cn-beijing.aliyuncs.com/img/Snipaste_2024-08-27_19-35-16.png',
						title: '师者'
					}
				],
				isActive: 1,
				leftNav: [{
						id: 1,
						title: "推荐榜"
					},
					// {
					// 	id: 2,
					// 	title: "全部"
					// }
				],
				ChapterList:[],//章节列表
				current_page:1, //当前页
				course_id:0 ,//教材id
				loading: "more",
				pagesize:10,
				previousChapterListLength:0
			};
		},
		onLoad(option){
			if (uni.getStorageSync('token')) {
				this.isLogin = true
			} else {
				  
				this.$nextTick(() => {
					this.$refs.login.modal = true;
				})
			}
			this.course_id = option.course_id
			this.getChapter({course_id:this.course_id})

		},
		methods: {
			scrollBottom(){
				if(this.loading != 'no-more'){
					this.loading = 'loading'
					this.pagesize+=10
					this.getChapter({course_id:this.course_id,pagesize:this.pagesize})
				}
			},
			//单击轮播图进入答题
			swiperClick(){
				//跳转答题页面
				uni.navigateTo({
					url:`/pages/paper/paper?course_id=${this.course_id}`
				})
			},
			succ(){
				this.getChapter({course_id:this.course_id})
			},
			NavTitle(id) {
				this.isActive = id
			},
			//搜索
			// search() {
			// 	console.log("搜索");
			// },
			//去学习
			CoureseStudy(id){
				uni.navigateTo({
					url:`/pages/CourseStudy/CourseStudy?course_id=${this.course_id}&chapter_id=${id}`
				})
				uni.hideHomeButton()
			},
			async getChapter(data){
				const res = await getChapterList(data)
				if(res.data.code == 1){
					this.current_page = res.data.data.current_page //当前页
					const newChapterList = res.data.data.data;
					// 判断长度是否增加
					if (newChapterList.length > this.previousChapterListLength) {
						this.loading = 'more'
					} else {
						this.loading = 'no-more'
					}
					// 更新ChapterList
					this.ChapterList = newChapterList;
					// 更新前一次的长度
					this.previousChapterListLength = newChapterList.length;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view-height{
		/* 底部安全边距 start*/
		.tn-tabbar-height {
			min-height: 120rpx;
			height: calc(140rpx + env(safe-area-inset-bottom) / 2);
			height: calc(140rpx + constant(safe-area-inset-bottom));
		}
		/* 页面高度减去包含状态栏、标题、tab组件的高度 */
		// height: calc(100vh - var(--status-bar-height) - 178rpx);
	.content {
		padding: 20rpx;
		margin: 20rpx;
		background: #fff;
		border-radius: 20rpx;

		.content_left {
			width: 20%;
			margin-right: 10rpx;
			border-right: 1px solid lightgray;

			.textFont {
				// font-weight: 500;
				// font-size: 28rpx;

				text {
					text-align: center;
					width: 100%;
					line-height: 50rpx;
				}

				.underline::after {
					content: "";
					display: block;
					width: 0;
					height: 2px;
					background-color: #3C76FC;
					// transition: width 0.3s ease;
				}

				.underline.active::after {
					width: 100%;
				}

				.active {
					color: #3C76FC;
				}

			}
		}

		.content_right {
			width: 80%;
			padding-left: 10rpx;
			.search {
				position: relative;
				border: 1px solid lightgray;
				border-radius: 30rpx;
				padding-left: 20rpx;
				width: 60%;
				height: 55rpx;
				line-height: 55rpx;

				input {
					width: 100%;
					height: 100%;
				}

				.Icon-search {
					position: absolute;
					top: 0;
					right: 20rpx;
				}

			}

			.heightScroll {
				height: calc(90vh - 220px);
				.content_right_chapter {
					width: 100%;
					border: 1px solid lightgray;
					padding: 10rpx;
					border-radius: 20rpx;
					padding-right: 30rpx;

					.chapter_img {
						width: 140rpx;
						height: 170rpx;

						image {
							border-radius: 10rpx;
							width: 100%;
							height: 100%;
						}
					}

					.textDetails {
						width: 200rpx;
						height: 100%;
						line-height: 50rpx;
						margin-left: 10rpx;

						.lablename{
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
				}
			}
		}
	}
	}
</style>